//
//  YD_purchaseCharms.m
//  SpeedDating
//
//  Created by isu on 16/12/15.
//  Copyright © 2016年 isu. All rights reserved.
//

#import "YD_purchaseCharms.h"

#import "YD_purchaseCharmsCellA.h"
#import "YD_purchaseCharmsCellB.h"
#import "YD_purchaseView.h"

//BeeCloudDelegate
@interface YD_purchaseCharms ()<UITableViewDelegate,UITableViewDataSource,btnClickDelegate>{
    int recordPayType_;
//    PayChannel _currentChannel;
    NSString * _billTitle;
    NSString *_billno;
}
/** baseView*/
@property (nonatomic, strong)UITableView *tableView;
/** userInfo2Model*/
@property (nonatomic, strong)UserInfocharmModel *userInfo2Model;
/** moneyStr*/
@property (nonatomic, strong)NSString *moneyStr;
@property (nonatomic, strong)NSString *payMoneyStr;

@end

@implementation YD_purchaseCharms

- (void)viewDidLoad {
    [super viewDidLoad];
    recordPayType_ = 1;
    

    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor =[UIColor whiteColor];
    self.title = @"购买钻石";
    [self makeTableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark--makeTableView
-(void)makeTableView{
    self.tableView  = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(self.view);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view);
    }];
    self.tableView.backgroundColor = Theme_Color_White;
    self.tableView.dataSource =self;
    self.tableView.delegate =self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.scrollEnabled = NO;
}

#pragma  mark--<UITableViewDelegate,UITableViewDataSource>
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 1;
    }else{
        return 4;
    }
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        YD_purchaseCharmsCellA *cell =[YD_purchaseCharmsCellA cellWithTableView:tableView];
        cell.currentCharmsLab.text = self.userInfo2Model.charm;
        return cell;
    }
    else {
        YD_purchaseCharmsCellB *cell = [YD_purchaseCharmsCellB cellWithTableView:tableView];
        if(indexPath.row==0){
            cell.showLab.text = @"10钻石";
            [cell.btn setTitle:@"￥10" forState:UIControlStateNormal];
            cell.btn.tag = indexPath.row;
        }
        else{
            if(indexPath.row==1){
                cell.showLab.text = @"50钻石";
                [cell.btn setTitle:@"￥50" forState:UIControlStateNormal];
            }else{
                if(indexPath.row==2){
                    cell.showLab.text =@"100钻石";
                    [cell.btn setTitle:@"￥90" forState:UIControlStateNormal];
                }else
                {
                    cell.showLab.text = @"800钻石";
                    [cell.btn setTitle:@"￥800" forState:UIControlStateNormal];
                }
            }
        }
        CGSize size = CGSizeMake(45, 45);
        UIGraphicsBeginImageContextWithOptions(size, NO,0.0);
        CGRect imageRect=CGRectMake(0.0, 0.0, size.width, size.height);
        [cell.imageView.image drawInRect:imageRect];
        cell.imageView.image=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        cell.btn.tag = indexPath.row;
        [cell.btn addTarget:self action:@selector(payClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}
//#pragma mark--payClick
//-(void)payClick:(UIButton*)sender{
//    self.moneyStr = @"";
//    self.payMoneyStr = @"";
//    switch (sender.tag) {
//        case 0:
//            self.moneyStr = @"10";
//            self.payMoneyStr = @"10";
//            break;
//        case 1:
//            self.moneyStr = @"50";
//            self.payMoneyStr = @"50";
//
//            break;
//        case 2:
//            self.moneyStr = @"100";
//            self.payMoneyStr = @"100";
//
//            break;
//        case 3:
//            self.moneyStr = @"800";
//            self.payMoneyStr = @"800";
//
//            break;
//        default:
//            break;
//    }
//    [YD_purchaseView showWithDelegate:self witnMoney:self.moneyStr PayMoney:self.payMoneyStr];
//}
//
//#pragma mark--btnClickDelegate
//-(void)didClickBtn:(UIButton *)sender{
//    switch (sender.tag) {
//        case 0:
//            
//            recordPayType_ = 0;
//            break;
//        case 1:
//            
//            recordPayType_ = 1;
//            break;
//        case 2:
//            
//            [YD_purchaseView Hide];
//            break;
//        default:
//            break;
//    }
//    if(sender.tag!=2){
//        return;
//    }
//    
//    if(recordPayType_==1){//支付宝
//        [self doPay:PayChannelAliApp];
//    }else{
//        //[SVProgressHUD showInfoWithStatus:@"暂不支持微信支付"];
//        
//        [self doPay:PayChannelWxApp];
//    }
//    
//}
//-(void)viewWillDisappear:(BOOL)animated{
//    [YD_purchaseView Hide];
//}
//
//#pragma mark - viewWillAppear
//-(void)viewWillAppear:(BOOL)animated{
//    [HcardHttpmanager getuserInfocharmsuccess:^(id sucess) {
//        if(sucess){
//            UserInfocharmModel  *model= (UserInfocharmModel *)sucess;
//            self.userInfo2Model =model;
//            [self.tableView reloadData];
//        }
//    } failure:^(NSError *error) {
//        
//    }];
//    
//    //设置delegate
//    [BeeCloud setBeeCloudDelegate:self];
//}
//
//#pragma mark - 微信、支付宝
//- (void)doPay:(PayChannel)channel {
//    _billno = [self genBillNo];
//    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
//    BCPayReq *payReq = [[BCPayReq alloc] init];
//    /**
//     *  支付渠道，PayChannelWxApp,PayChannelAliApp,/(PayChannelUnApp,PayChannelBaiduApp)(略)
//     */
//    //测试：
//    _billTitle = @"贴脸儿钻石充值";
//    payReq.channel = channel; //支付渠道
//    payReq.title = _billTitle;//订单标题
//    //payReq.totalFee =[NSString stringWithFormat:@"%lf",[self.moneyStr doubleValue]*0.01] ;
//    payReq.totalFee = _payMoneyStr;//订单价格;
//    payReq.billNo = _billno;//商户自定义订单号
//    payReq.scheme = @"AlipayShanyue";//URL Scheme,在Info.plist中配置; 支付宝,银联必有参数
//    payReq.billTimeOut = 300;//订单超时时间
//    payReq.viewController = self; //银联支付和Sandbox环境必填
//    payReq.optional = dict;//商户业务扩展参数，会在webhook回调时返回
//    [BeeCloud sendBCReq:payReq];
//}
//
//#pragma mark - 生成订单号
//- (NSString *)genBillNo {
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"yyyyMMddHHmmssSSS"];
//    return [formatter stringFromDate:[NSDate date]];
//}
//
//#pragma mark - BCPay回调
//- (void)onBeeCloudResp:(BCBaseResp *)resp {
//    switch (resp.type) {
//        case BCObjsTypePayResp:
//        {
//            // 支付请求响应
//            BCPayResp *tempResp = (BCPayResp *)resp;
//            if (tempResp.resultCode == 0) {
//                BCPayReq *payReq = (BCPayReq *)resp.request;
//                //百度钱包比较特殊需要用户用获取到的orderInfo，调用百度钱包SDK发起支付
//                if (payReq.channel == PayChannelBaiduApp && ![BeeCloud getCurrentMode]) {
//                    
//                } else {
//                    //test：模拟支付（ownService）
//                    NSString *note = @"支付宝支付";
//                    NSString *transNo =_billno;
//                    [HcardHttpmanager inOutCharmWithCharm:self.moneyStr withCost:_payMoneyStr withNote:note withType:KInCharmType withTransNo:transNo withCharm1:@"" withfirstCharge:@"" success:^(id result) {
//                        if(result){
//                            //微信、支付宝、银联支付成功
//                           // [self showAlertView:resp.resultMsg];
//                            [self.navigationController popViewControllerAnimated:YES];
//                        }
//                    } failure:^(NSError *error) {
//                        
//                    }];
// 
//                }
//            } else {
//                //支付取消或者支付失败
//                [self showAlertView:[NSString stringWithFormat:@"%@ : %@",tempResp.resultMsg, tempResp.errDetail]];
//            }
//        }
//            break;
//        default:
//            break;
//    }
//}

- (void)showAlertView:(NSString *)msg {
    
    
    
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

//礼物修改购买方式

@end
